package com.netscenery.yang.model;

import org.jdom2.Attribute;
import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;

public class BelongsTo extends ModelElement{
	private String prefix;
	//private MainModule belongs;
	
	public BelongsTo(Env env,String name, String prefix){
		this.env = env;
		this.setName(name);		
		this.setPrefix(prefix);
	}
	public BelongsTo(){
		
	}
	public String getPrefix() {
		return prefix;
	}



	public void setPrefix(String prefix) {
		if (null != this.env){
			if(this.env.hasPrefix(prefix)){
				return;
			}
			
		}
		this.prefix = prefix;
		if(null != env){
			env.addPrefix(prefix);
		}
	}

	@Override
	public Element toXML() {

		
		Element belongsto = new Element(XMLTag.XML_TAG_BELONGSTO);
		Element name = new Element(XMLTag.XML_TAG_NAME);
		name.setText(this.name);
		belongsto.addContent(name);
		if (null != this.prefix){
			Element prefix = new Element(XMLTag.XML_TAG_PREFIX);
			prefix.setText(this.prefix);
			belongsto.addContent(prefix);
		}
		
		
		return belongsto;
	}

	@Override
	public int readXML(Element element) {
		if (null == element){
			return MODELELEMENT_OK;
		}
		if (element.getName().equals(XMLTag.XML_TAG_PREFIX)){
			this.setPrefix(element.getText());
		}
		else if (element.getName().equals(XMLTag.XML_TAG_NAME)){
			return MODELELEMENT_OK;
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}




	@Override
	public YangStatement toYang() {

		YangStatement belongsTo = new YangStatement(YangStatement.YANG_STATEMENT_BELONGSTO,this.name);
		if (null != this.prefix){
			YangStatement prefix = new YangStatement(YangStatement.YANG_STATEMENT_PREFIX,this.prefix);
			belongsTo.addChild(prefix);
		}

		return belongsTo;
	}



	@Override
	public int readYANG(YangStatement statement) {
		if (null == statement){
			return MODELELEMENT_OK;
		}
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_PREFIX)){
			this.setPrefix(statement.getArgument());
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}


	@Override
	public Validator validate() {
		Validator validator = new Validator();
		if (isNullString(name)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"missing name"));
		}
		if (isNullString(prefix)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"missing prefix"));
		}
		
//		if (null == this.belongs){
//			return false;
//		}
//		
//		if (!this.name.equals(belongs.getName())){
//			return false;
//		}
		Validator v = super.validate();
		validator.copy(v);
		
		return validator;
	}


	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Element toYin() {
		Element belongsto= new Element(YangKeyword.BELONGSTO.getKeyword(),env.getNs());
		Attribute module = new Attribute(YangKeyword.MODULE.getKeyword(),this.name);
		belongsto.setAttribute(module);
		if (!isNullString(this.prefix)){
			Element prefix= new Element(YangKeyword.PREFIX.getKeyword(),env.getNs());
			Attribute value = new Attribute(YangKeyword.VALUE.getKeyword(),this.prefix);
			prefix.setAttribute(value);
			belongsto.addContent(prefix);
		}
		
		return belongsto;
	}

	@Override
	public int readYin(Element element) throws ModelException {
		if (null == element){
			return MODELELEMENT_OK;
		}
		if (element.getName().equals(YangKeyword.PREFIX.getKeyword())){
			this.setPrefix(element.getAttributeValue(YangKeyword.VALUE.getKeyword()));
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}


	
	
}
